package com.dq.controller.op.opboss.org;

import com.alibaba.fastjson.JSONObject;
import com.dq.exception.customException.AdminException;
import com.dq.controller.in.user.OrgUserQuery;
import com.dq.controller.in.org.OrgQuery;
import com.dq.vo.ListDataVo;
import com.dq.vo.Vo;

import com.dq.domain.org.Org;
import com.dq.domain.org.centre.OrgDeptPositionCentre;
import com.dq.domain.sys.section.SysSection;
import com.dq.domain.sys.user.user.OrgUser;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.service.AllService;
import com.dq.utils.BeanNullUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuhui
 * @date 2019/11/22 0022 11:00:39
 */
@RestController
@RequestMapping("/sys/org")
@Api(description = "boss 门店管理")
public class BossOrgController extends AllService {

    @GetMapping("/orgUser/delete/{orgUserId}")
    @ApiOperation("门店用户删除")
    public Vo orgUserDelete(@PathVariable Long orgUserId)throws AdminException{
        OrgUser orgUser = dao.get(OrgUser.class, orgUserId);
        if (null == orgUser) {
            throw new AdminException("门店账号不存在");
        }

        orgUser.setDeleted(true);

        orgUserService.update(orgUser);
        return new Vo(HttpStatus.OK);
    }

    @GetMapping("/orgPositionList/{orgId}")
    @ApiOperation("查询门店岗位列表")
    public Vo orgPositionList(@PathVariable Long orgId) throws AdminException{
        Org org = dao.get(Org.class,orgId);
        if(org == null){
            throw new AdminException("请先绑定门店");
        }
        List<OrgDeptPositionCentre> orgDeptPositionCentres =orgDeptPositionRepository.findByDeletedAndOrgOrderByIdDesc(false, org);
        List<Map> maps = new ArrayList<>();
        orgDeptPositionCentres.forEach(OrgDeptPositionCentre -> {
            Map<String,Object> map = new HashMap<>();
            map.put("name",OrgDeptPositionCentre.getOrgPosition().getPostName());
            map.put("id",OrgDeptPositionCentre.getOrgPosition().getId());
            maps.add(map);
        });
        PageInfo pageInfo = new PageInfo(maps);
        return new Vo(pageInfo);
    }


    @GetMapping("/boosOrgPositionList")
    @ApiOperation("boss查询自己门店岗位列表")
    public Vo boosOrgPositionList() throws AdminException{
        SysUser sysUser = shiroService.getUser();
        SysSection section = sysUser.getSection();
        if(section == null){
            throw new AdminException("请先绑定门店");
        }
        List<OrgDeptPositionCentre> orgDeptPositionCentres =orgDeptPositionRepository.findByDeletedAndOrgOrderByIdDesc(false, section);
        List<Map> maps = new ArrayList<>();
        orgDeptPositionCentres.forEach(OrgDeptPositionCentre -> {
            Map<String,Object> map = new HashMap<>();
            map.put("name",OrgDeptPositionCentre.getOrgPosition().getPostName());
            map.put("id",OrgDeptPositionCentre.getOrgPosition().getId());
            maps.add(map);
        });
        PageInfo pageInfo = new PageInfo(maps);
        return new Vo(pageInfo);
    }

    @GetMapping("/orgOrgPositionList")
    @ApiOperation("门店查询自己门店岗位列表")
    public Vo orgOrgPositionList() throws AdminException{
        SysUser sysUser = shiroService.getUser();
        SysSection org = sysUser.getSection();
        if(org == null){
            throw new AdminException("请先绑定门店");
        }
        List<OrgDeptPositionCentre> orgDeptPositionCentres =orgDeptPositionRepository.findByDeletedAndOrgOrderByIdDesc(false, (Org) org);
        List<Map> maps = new ArrayList<>();
        orgDeptPositionCentres.forEach(OrgDeptPositionCentre -> {
            Map<String,Object> map = new HashMap<>();
            map.put("name",OrgDeptPositionCentre.getOrgPosition().getPostName());
            map.put("id",OrgDeptPositionCentre.getOrgPosition().getId());
            maps.add(map);
        });
        return new Vo(maps);
    }
}
